import { RootThunkAction } from '@/types/store'
import request from '@/utils/request'
import { ApiResponse, LoginAction, LoginForm, token } from '@/types/data'
import { removeToken, setToken } from '@/utils/storage'
export function login (datas:LoginForm):RootThunkAction {
  return async (dispatch) => {
    const res = await request.post<ApiResponse<token>>('/authorizations', datas)
    setToken(res.data.data)
    dispatch({
      type: 'login/Token',
      payload: res.data.data
    })
  }
}

export function getCode (mobile:string) {
  return async () => {
    await request.get('/sms/codes/' + mobile)
  }
}
export const logout = ():LoginAction => {
  removeToken()
  return {
    type: 'login/logout'
  }
}
